package com.xinweisi.dataserver.module.datastatistics.bettingrecord;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinweisi.dataserver.common.checkDBName.CheckDBNameService;
import com.xinweisi.dataserver.common.domain.PageResultDTO;
import com.xinweisi.dataserver.common.domain.ResponseDTO;
import com.xinweisi.dataserver.module.datastatistics.bettingrecord.dto.BettingRecordQueryDTO;
import com.xinweisi.dataserver.module.datastatistics.bettingrecord.entity.BettingRecordEntity;
import com.xinweisi.dataserver.module.datastatistics.bettingrecord.entity.MGBettingEntity;
import com.xinweisi.dataserver.module.datastatistics.bettingrecord.entity.PTBettingEntity;
import com.xinweisi.dataserver.module.datastatistics.bettingrecord.vo.*;
import com.xinweisi.dataserver.module.memberdata.user.domain.entity.UserEntity;
import com.xinweisi.dataserver.util.CalculationUtil;
import com.xinweisi.dataserver.util.SmartPageUtil;
import com.xinweisi.dataserver.util.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * MemberBettingRecordService
 *
 * @author lijie1
 * @version 1.0
 * @date 2020/7/29 0029 14:16
 */
@Slf4j
@Service
public class MemberBettingRecordService {
    @Autowired
    private MemberBettingRecordDao memberBettingRecordDao;

    @Autowired
    private CheckDBNameService checkDBNameService;

    /**
     * 投注记录查询
     * @param queryDTO
     * @author 
     * @date 2020/10/16 0016 9:59 
     * @return com.xinweisi.dataserver.common.domain.ResponseDTO<com.xinweisi.dataserver.module.datastatistics.bettingrecord.vo.BettingRecordBaseVO>
     */
    public ResponseDTO<BettingRecordBaseVO> getData(BettingRecordQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<BettingRecordVO> voList = new ArrayList<>();
        double betting = 0.0;//下注总额
        double delivery = 0.0;//派送总额
        for (int i=0;i<queryDTO.getDBNameList().size();i++){
            queryDTO.setDBName(queryDTO.getDBNameList().get(i));
            int flag = checkDBNameService.checkDBName(queryDTO.getDBName());
            if(flag==0){
                continue;
            }
            List<BettingRecordEntity> entityList = memberBettingRecordDao.selectBehaviorDailyList(queryDTO);
            for (BettingRecordEntity entity:entityList) {
                BettingRecordVO bettingRecordVO = new BettingRecordVO();
                UserEntity en = UserUtil.userMap.get(entity.getUserId());//获取交易商ByUserId
                bettingRecordVO.setGameAccount(en.getAccount());
                bettingRecordVO.setGameAccountName(en.getAccountName());
                bettingRecordVO.setBettingTime(entity.getGameTime());
                bettingRecordVO.setGameName(entity.getGameType());
                bettingRecordVO.setOrderId(entity.getOrderId());
                String balanceChange = entity.getBalanceChange();
                int multiple = entity.getMultiple();
                if(multiple==0){
                    multiple = 1;
                }
                //用户资金变化
                String[] balances1 = balanceChange.split("_");
                Double fundOld = "-1".equals(balances1[0]) ? 0.0 : Double.parseDouble(balances1[0]);
                Double fundNew = "-1".equals(balances1[1]) ? 0.0 : Double.parseDouble(balances1[1]);
                double betResult = fundNew - fundOld;
                double betAmount =0.0;
                if(balances1.length==3){
                    betAmount = Double.parseDouble(balances1[2]);
                }else {
                    betAmount = betResult;
                }
                if(balances1.length==4){
                    betAmount = Double.parseDouble(balances1[2]);
                    betResult = Double.parseDouble(balances1[3]);
                }
                if (betAmount >= 0) {
                    betAmount = betAmount / multiple;
                } else {
                    betAmount = (betAmount * -1) / multiple;
                }
                betting = betting + betAmount;
                if(betResult>0){
                    delivery = delivery + betResult;
                }
                bettingRecordVO.setBetAmount(betAmount);
                bettingRecordVO.setBetResult(betResult);
                voList.add(bettingRecordVO);
            }
        }
        page.setTotal(voList.size());
        PageResultDTO<BettingRecordVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, SmartPageUtil.startPage(voList,queryDTO.getPageNum(),queryDTO.getPageSize()), BettingRecordVO.class);
        BettingRecordBaseVO entity = new BettingRecordBaseVO();
        entity.setBettingRecordVOS(pageResultDTO);
        entity.setTotal(voList.size());
        entity.setBetting(betting);
        if(delivery<0){
            delivery = 0.0;
        }
        entity.setDelivery(delivery);
        return ResponseDTO.succData(entity);
    }

    /**
     * pt投注记录查询
     * @param queryDTO
     * @author 
     * @date 2020/10/16 0016 9:59 
     * @return com.xinweisi.dataserver.common.domain.ResponseDTO<com.xinweisi.dataserver.module.datastatistics.bettingrecord.vo.PTBettingVO>
     */
    public ResponseDTO<PTBettingBaseVO> getPTData(BettingRecordQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<PTBettingEntity> voList = new ArrayList<>();
        for (int i=0;i<queryDTO.getDBNameList().size();i++) {
            queryDTO.setDBName(queryDTO.getDBNameList().get(i));
            int flag = checkDBNameService.checkDBName(queryDTO.getDBName());
            if (flag == 0) {
                continue;
            }
            List<PTBettingEntity> entityList = memberBettingRecordDao.selectPTBettingList(queryDTO);
            voList.addAll(entityList);
        }
        page.setTotal(voList.size());
        PageResultDTO<PTBettingVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, SmartPageUtil.startPage(voList,queryDTO.getPageNum(),queryDTO.getPageSize()), PTBettingVO.class);
        for (PTBettingVO obj:pageResultDTO.getList()) {
            UserEntity en = UserUtil.userMap.get(obj.getPlayerName());//获取交易商ByUserId
            obj.setUserName(en.getAccount());
        }
        PTBettingBaseVO ptBettingBaseVO = new PTBettingBaseVO();
        ptBettingBaseVO.setBettingRecordVOS(pageResultDTO);
        return ResponseDTO.succData(ptBettingBaseVO);
    }

    /**
     *  MG投注记录查询
     * @param queryDTO
     * @author
     * @date 2020/10/16 0016 10:39
     * @return com.xinweisi.dataserver.common.domain.ResponseDTO<com.xinweisi.dataserver.module.datastatistics.bettingrecord.vo.PTBettingBaseVO>
     */
    public ResponseDTO<MGBettingBaseVO> getMGData(BettingRecordQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<MGBettingEntity> voList = new ArrayList<>();
        double betting = 0.0;//下注总额
        double delivery = 0.0;//派送总额
        for (int i=0;i<queryDTO.getDBNameList().size();i++) {
            queryDTO.setDBName(queryDTO.getDBNameList().get(i));
            int flag = checkDBNameService.checkDBName(queryDTO.getDBName());
            if (flag == 0) {
                continue;
            }
            List<MGBettingEntity> entityList = memberBettingRecordDao.selectMGBettingList(queryDTO);
            for (MGBettingEntity obj:entityList) {
                obj.setCreateDateUTC(obj.getCreateDateUTC().replace("T"," "));
                obj.setGameStartTimeUTC(obj.getGameStartTimeUTC().replace("T"," "));
                obj.setGameEndTimeUTC(obj.getGameEndTimeUTC().replace("T"," "));
                betting = CalculationUtil.add(betting,obj.getBetAmount());
                delivery = CalculationUtil.add(delivery,obj.getPayoutAmount());
            }
            voList.addAll(entityList);
        }
        page.setTotal(voList.size());
        PageResultDTO<MGBettingVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, SmartPageUtil.startPage(voList,queryDTO.getPageNum(),queryDTO.getPageSize()), MGBettingVO.class);
        for (MGBettingVO obj:pageResultDTO.getList()) {
            UserEntity en = UserUtil.userMap.get(obj.getPlayerId());//获取交易商ByUserId
            obj.setUserName(en.getAccount());
        }
        MGBettingBaseVO ptBettingBaseVO = new MGBettingBaseVO();
        ptBettingBaseVO.setTotal(voList.size());
        ptBettingBaseVO.setBetting(betting);
        ptBettingBaseVO.setDelivery(delivery);
        ptBettingBaseVO.setBettingRecordVOS(pageResultDTO);
        return ResponseDTO.succData(ptBettingBaseVO);
    }
}
